package com.xbongbong.pro.dataset.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.ExportFormDataVO;
import com.xbongbong.paas.pojo.vo.SearchDataInConditionVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.dataset.service.DataSetChartResultService;
import com.xbongbong.pro.dataset.service.DataSetSubFormService;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.pojo.dto.DataSetSubFormListDTO;
import com.xbongbong.pro.statistic.pojo.vo.ChartSubFormListVO;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartLinkDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartPenetrateResultDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartResultDTO;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartResultVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 图表中心调用数据集返回
 * @ClassName DataSetResultController
 * @Author 梁鲁江
 * 创建时间 2022/9/16 9:20
 * 修改时间  2022/9/16 9:20
 **/
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/dsValue")
public class DataSetResultController {
    @Resource
    private DataSetChartResultService dataSetChartResultService;
    @Resource
    private DataSetSubFormService dataSetSubFormService;
    @RequestMapping(value = "/result", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String dsResult(@RequestBody ChartResultDTO chartResultDTO, BindingResult br) {
        XbbResponse<ChartResultVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //请求参数后处理，比如del、enable
                chartResultDTO.postProcess();
                ChartResultVO chartResultVO = dataSetChartResultService.chartResult(chartResultDTO);
                response = new XbbResponse<>(chartResultVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 获取子表单数据
     * @param dataSetSubFormListDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/9/29 9:06
     */
    @RequestMapping(value = "/subFormList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public String subFormList(@RequestBody @Valid DataSetSubFormListDTO dataSetSubFormListDTO, BindingResult br) {
        XbbResponse<ChartSubFormListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartSubFormListVO chartSubFormListVO = dataSetSubFormService.chartGetSubFormList(dataSetSubFormListDTO);
                response = new XbbResponse<>(chartSubFormListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取数据集自定义图表穿透结果
     *
     * @param chartPenetrateResultDTO
     * @param br
     * @return java.lang.String
     * @author zyy
     * @date 2022/10/28
     * @version v1.0
     */
    @RequestMapping(value = "/result/penetrate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartPenetrateResult(@RequestBody @Valid ChartPenetrateResultDTO chartPenetrateResultDTO, BindingResult br) {
        XbbResponse<ChartResultVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //请求参数后处理，比如del、enable
                chartPenetrateResultDTO.postProcess();
                ChartResultVO chartResultVO = dataSetChartResultService.chartPenetrateResult(chartPenetrateResultDTO);
                response = new XbbResponse<>(chartResultVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 图表中心关联查询的处理
     * @param chartLinkDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/chartLinkResultresult", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartLinkResult(@RequestBody @Valid ChartLinkDTO chartLinkDTO, BindingResult br) {
        XbbResponse<SearchDataInConditionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SearchDataInConditionVO searchDataInConditionVO = dataSetChartResultService.chartLinkResult(chartLinkDTO);
                response = new XbbResponse<>(searchDataInConditionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
